package com.lt.system;

import android.annotation.TargetApi;
import android.os.Build;
import android.view.View;

import com.google.android.material.snackbar.Snackbar;

public class SnackbarHelper {
    //工具类当前持有的Snackbar实例
    private static Snackbar mSnackbar = null;

    private SnackbarHelper(Snackbar snackbar) {
        this.mSnackbar = snackbar;
    }

    /**
     * 设置TextView(@+id/snackbar_text)中文字的对齐方式 居中
     *
     * @return
     */
    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
    public SnackbarHelper messageCenter() {
        if (mSnackbar == null) {
            return this;
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
//            TextView message = mSnackbar.getView().findViewById(R.id.snackbar_text);
//            if (message != null) {
//                //View.setTextAlignment需要SDK>=17
//                message.setTextAlignment(View.TEXT_ALIGNMENT_GRAVITY);
//                message.setGravity(Gravity.CENTER);
//            }
        }
        return this;
    }

    public static SnackbarHelper make(View view, String message, int duration) {
        mSnackbar = Snackbar.make(view, message, duration);
        return new SnackbarHelper(mSnackbar);
    }

    public static SnackbarHelper make(View view, String message) {
        mSnackbar = Snackbar.make(view, message, Snackbar.LENGTH_LONG);
        return new SnackbarHelper(mSnackbar);
    }


    public void show() {
        if (mSnackbar != null) {
            mSnackbar.show();
        }
    }

}
